package com.gamiao.gwt.demo.useSpring.server;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.springframework.transaction.annotation.Transactional;

import com.gamiao.gwt.demo.useSpring.client.PersonClient;
import com.gamiao.gwt.demo.useSpring.client.UserService;

@Transactional
public class UserServiceImpl implements UserService {

	private EntityManager em;

	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	}

	private EntityManager getEntityManager() {
		return em;
	}

	private static ThreadLocal<ServletRequest> servletRequest = new ThreadLocal<ServletRequest>();

	private static ThreadLocal<ServletResponse> servletResponse = new ThreadLocal<ServletResponse>();

	public void setRequest(ServletRequest request) {
		servletRequest.set(request);
	}

	public void setResponse(ServletResponse request) {
		servletResponse.set(request);
	}

	protected HttpServletRequest getRequest() {
		return (HttpServletRequest) servletRequest.get();
	}

	private PersonClient[] convertList(List<PersonDB> list) {
		Object[] personDBs = (Object[]) list.toArray();
		int personDBsSize = personDBs.length;
		PersonClient[] personClients = new PersonClient[personDBsSize];
		for (int i = 0; i < personDBsSize; i++) {
			PersonDB personDB = (PersonDB) personDBs[i];
			PersonClient personClient = new PersonClient();
			personClient.setId(personDB.getId().intValue());
			personClient.setFirstName(personDB.getFirstName());
			personClient.setLastName(personDB.getLastName());
			personClients[i] = personClient;
		}
		return personClients;
	}

	@SuppressWarnings("unchecked")
	public PersonClient[] findAll() {
		Query query = getEntityManager()
				.createQuery("select p FROM PersonDB p");
		List<PersonDB> list = query.getResultList();
		return convertList(list);
	}

	public void save(PersonClient personClient) {
		PersonDB personDB = new PersonDB();
		personDB.setId(personClient.getId() == 0 ? null : new Integer(
				personClient.getId()));
		personDB.setFirstName(personClient.getFirstName());
		personDB.setLastName(personClient.getLastName());
		if (personDB.getId() == null) {
			// new
			em.persist(personDB);
		} else {
			// update
			em.merge(personDB);
		}
	}

	public void remove(int id) {
		PersonDB personDB = findPersonDB(id);
		if (personDB != null) {
			em.remove(personDB);
		}
	}

	public PersonClient find(int id) {
		PersonDB personDB = em.find(PersonDB.class, id);
		PersonClient personClient = new PersonClient();
		personClient.setId(personDB.getId().intValue());
		personClient.setFirstName(personDB.getFirstName());
		personClient.setLastName(personDB.getLastName());
		return personClient;
	}

	private PersonDB findPersonDB(int id) {
		return em.find(PersonDB.class, id);
	}
}
